package com.topscore.uke.ui.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.topscore.uke.R;
import com.topscore.uke.mvp.contractors.MemberBoundContractor;
import com.topscore.uke.mvp.presenters.MemberBoundPresenter;
import com.topscore.uke.ui.ToolBarCompatActivity;
import com.topscore.utillibrary.util.CountDownUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MemberBindingActivity extends ToolBarCompatActivity implements MemberBoundContractor.View, CountDownUtil.OnCountDownListener, View.OnClickListener {

    @Bind(R.id.phone_edt)
    EditText phoneEdt;
    @Bind(R.id.get_verification_tv)
    TextView getVerificationTv;
    @Bind(R.id.get_verification_btn)
    RelativeLayout getVerificationBtn;
    @Bind(R.id.binding_btn)
    Button bindingBtn;
    @Bind(R.id.verify_edt)
    EditText verifyEdt;

    private MemberBoundContractor.Presenter mPresenter;

    private CountDownUtil mCountDownUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meber_binding);
        ButterKnife.bind(this);
        mPresenter = new MemberBoundPresenter(this);
        setCentralTitle("绑定会员");
        initViews();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_skip, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_skip:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initViews() {
        mCountDownUtil = new CountDownUtil(45, this);
        getVerificationBtn.setOnClickListener(this);
        bindingBtn.setOnClickListener(this);
    }

    @Override
    public EditText getPhoneEdt() {
        return phoneEdt;
    }

    @Override
    public EditText getVerificationEdt() {
        return verifyEdt;
    }

    @Override
    public void setupButton(boolean isClickable) {
        if (isClickable) {
            bindingBtn.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        } else {
            bindingBtn.setBackgroundColor(Color.parseColor("#dcdcdc"));
        }
        bindingBtn.setClickable(isClickable);
    }

    @Override
    public void onGetVerifySuccess() {
        mCountDownUtil.start();
        Toast.makeText(mContext, "验证码已发送，请稍等", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onBindingSuccess() {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onLoading(String loadingMessage) {
        showLoadingDialog(loadingMessage);
    }

    @Override
    public void onLoadFinished() {
        dismissLoadingDialog();
    }

    @Override
    public void onFail(int code, String errorMessage) {
        Toast.makeText(mContext, errorMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCount(int seconds) {
        getVerificationBtn.setClickable(false);
        getVerificationTv.setTextColor(Color.parseColor("#999999"));
        getVerificationTv.setText(seconds + "(S)");
    }

    @Override
    public void onCountDownComplete() {
        getVerificationTv.setTextColor(getResources().getColor(R.color.colorPrimary));
        getVerificationTv.setText("获取验证码");
        getVerificationBtn.setClickable(true);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.get_verification_btn:
                mPresenter.getVerificationCode();
                break;
            case R.id.binding_btn:
                mPresenter.bindMember();
                break;
        }
    }
}
